//终端业务机会测试类
@isTest
private class OppTerminalControllerTest {
    static testMethod void testMethod1() {
        	

        RecordType rt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'TerminalCustomer'];
       // RecordType mrt = [select Id from RecordType where SObjectType = 'Account' and DeveloperName = 'Maccura'];

        RecordType rtopp = [select Id from RecordType where SObjectType = 'Opportunity' and DeveloperName = 'CustomerOpportunity'];

        Province__c pro = TestUtils.createProvince();
        insert pro;

        City__c c = TestUtils.createCity();
        c.Province__c = pro.Id;
        insert c;

        District__c d = TestUtils.createDistrict();
        d.City__c = c.Id;
        insert d;

        // 客户
        Account acc = new Account();
        acc.Name = 'test customer';
        acc.Status__c = '审核通过';
        acc.Level__c = '一级医院';
        acc.RecordTypeId = rt.Id;
        insert acc;

        PageReference page = new PageReference('/apex/OppTerminal?id=' + acc.Id );
        System.Test.setCurrentPage(page);
        OppTerminalController oppController = new OppTerminalController();

        oppController.init();
        Map<String,Object> saveMap =new Map<String,Object>();
        Map<String,String> oppMap =new Map<String,String>();
        oppMap.put('IntentionalProductRange__c', '试剂');
       // oppMap.put('ProjectTypeNew__c', '单项目');
        oppMap.put('SalesType__c', '单一销售');
        oppMap.put('ProjStartDate__c', '2019-12-12');
        oppMap.put('CloseDate', '2019-12-31');
        oppMap.put('NewName__c', '1');
        oppMap.put('Department__c', '检验科');
        oppMap.put('SalesPriceSumYear__c', '1');
        oppMap.put('CooperationMode__c', '整体配送');
        oppMap.put('WinRatio__c', '100');

       // 'SalesType__c','ProjStartDate__c','CloseDate','SalesType__c','NewName__c',
       // 'Department__c','SalesPriceSumYear__c','CooperationMode__c','WinRatio__c'
        Map<String,Object> dataMap =new Map<String,Object>();
        dataMap.put('Opportunity', oppMap);


        Map<String,String> acc1Map =new Map<String,String>();

        acc1Map.put('Category__c', '综合医院');
        acc1Map.put('Level__c', '一级医院');
        acc1Map.put('CustomerAttribute__c', '公立医疗机构');
        acc1Map.put('Province__c', pro.Id);
        acc1Map.put('City__c', c.Id);
        acc1Map.put('District__c', d.Id);
        acc1Map.put('Address__c', 'xxxx');
        acc1Map.put('SpecialDepart__c', '检验科');
        acc1Map.put('BedNumOpen__c', '100');
        acc1Map.put('LastYearRevenue__c', '100');
        acc1Map.put('Phone', '10086');

        
        dataMap.put('Account', acc1Map);


        saveMap.put('base', dataMap);

        oppController.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');

        oppController.upgradeToNext();


        Opportunity opp = [select Id ,StageName from Opportunity  limit 1];

        System.debug(opp);

           //产品1  
        Product__c p1 = TestUtils.createInst();
        p1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c') ;
        insert p1;

         IntentProduct__c ip1 = TestUtils.createIntentProduct();
        ip1.Product__c = p1.Id;
        ip1.Opportunity__c = opp.Id;
        ip1.Comment__c = 'Tste===============';
        insert ip1;

        //意向 

        PageReference page1 = new PageReference('/apex/OppTerminal?id=' + opp.Id );
        System.Test.setCurrentPage(page1);

          //Set<String>  fields = new Set<String>{'Category__c','Level__c','CustomerAttribute__c','Province__c','City__c','District__c','Address__c'
        //,'SpecialDepart__c','BedNumOpen__c','LastYearRevenue__c'};
        OppTerminalController oppControllerEdit = new OppTerminalController();
        oppControllerEdit.init();

        Map<String,String> accMap =new Map<String,String>();

        accMap.put('Category__c', '综合医院');
        accMap.put('Level__c', '一级医院');
        accMap.put('CustomerAttribute__c', '公立医疗机构');
        accMap.put('Province__c', pro.Id);
        accMap.put('City__c', c.Id);
        accMap.put('District__c', d.Id);
        accMap.put('Address__c', 'xxxx');
        accMap.put('SpecialDepart__c', '检验科');
        accMap.put('BedNumOpen__c', '100');
        accMap.put('LastYearRevenue__c', '100');
        accMap.put('Phone', '10086');

        
        dataMap.put('Account', accMap);

        List<Object> depts = new List<Object>();

        Map<String,String> deptMap =new Map<String,String>();


		deptMap.put('deptId',[select Id from Department__c where TerminalCustomer__c =:acc.Id limit 1 ].Id  );
		deptMap.put('name','检验科' );
		deptMap.put('accId',acc.Id );
		deptMap.put('lastProfit','100');
		deptMap.put('lastBuy','100' );
		depts.add(deptMap);
        saveMap.put('dept', depts);

		List<Object> contacts  = new List<Object>();

        Map<String,String> contactMap =new Map<String,String>();
        contactMap.put('contactId',null );
        contactMap.put('name','院长' );
        contactMap.put('phone','11' );
        contactMap.put('accId',acc.Id );
        contactMap.put('post','院长' );

        contactMap.put('dept','医院办公室' );
      
        contacts.add(contactMap);


        Map<String,String> contactMap1 =new Map<String,String>();
        contactMap1.put('contactId',null );
        contactMap1.put('name','分管院长' );
        contactMap1.put('phone','131' );
        contactMap1.put('accId',acc.Id );
        contactMap1.put('post','分管院长(设备)' );

        contactMap1.put('dept','医院办公室' );
        contacts.add(contactMap1);

        Map<String,String> contactMap2 =new Map<String,String>();
        contactMap2.put('contactId',null );
        contactMap2.put('name','科长/主任' );
        contactMap2.put('phone','132' );
        contactMap2.put('accId',acc.Id );
        contactMap2.put('post','科长/主任' );

        contactMap2.put('dept','财务科/计财处' );
        contacts.add(contactMap2);


         Map<String,String> contactMap3 =new Map<String,String>();
        contactMap3.put('contactId',null );
        contactMap3.put('name','检验科科长/主任' );
        contactMap3.put('phone','133' );
        contactMap3.put('accId',acc.Id );
        contactMap3.put('post','科长/主任' );

        contactMap3.put('dept','检验科' );

        contacts.add(contactMap3);

         Map<String,String> contactMap4 =new Map<String,String>();
        contactMap4.put('contactId',null );
        contactMap4.put('name','设备科科长/主任' );
        contactMap4.put('phone','134' );
        contactMap4.put('accId',acc.Id );
        contactMap4.put('post','科长/主任' );

        contactMap4.put('dept','设备科/工程部/维修科' );
        contacts.add(contactMap4);


        saveMap.put('contact', contacts);


         oppControllerEdit.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');

        oppControllerEdit.upgradeToNext();


        Quote q = new Quote();
        q.Name = '报价';
        q.OpportunityId = opp.Id;
        q.Status = '申请中';
        insert q;

         //立项

        PageReference page2 = new PageReference('/apex/OppTerminal?id=' + opp.Id );
        System.Test.setCurrentPage(page2);

          //Set<String>  fields = new Set<String>{'Category__c','Level__c','CustomerAttribute__c','Province__c','City__c','District__c','Address__c'
        //,'SpecialDepart__c','BedNumOpen__c','LastYearRevenue__c'};
        OppTerminalController oppControllerEdit1 = new OppTerminalController();
        oppControllerEdit1.init();
        oppControllerEdit1.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');

        oppControllerEdit1.upgradeToNext();



         PageReference page3 = new PageReference('/apex/OppTerminal?id=' + opp.Id +'&drop=true');
        System.Test.setCurrentPage(page3);

        // Set<String>  fields = new Set<String>{'LostReason__c','Successfulcompany__c','Winningcompanychannel__c'
        //'LostReasoneDesc__c','SuccessfulModel__c','WhetherParticipate__c','ContractTime__c','SuccessfulBrand__c'};

        OppTerminalController oppControllerEdit2 = new OppTerminalController();
        oppControllerEdit2.init();
        oppMap.clear();
        oppMap.put('LostReason__c', 'xxx');
        oppMap.put('Successfulcompany__c', 'xxx');
        oppMap.put('Winningcompanychannel__c', 'xxx');
        oppMap.put('LostReasoneDesc__c', 'xxx');
        oppMap.put('SuccessfulModel__c', 'xxx');
        oppMap.put('WhetherParticipate__c', '是');
        oppMap.put('ContractTime__c', 'xxx');
        oppMap.put('SuccessfulBrand__c', 'xxx');

        oppControllerEdit2.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');
        oppControllerEdit2.upgradeToNext();


      








       
    }
}